package com.cat.model.biz.service.api.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.aspectj.asm.AsmManager.ModelInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.json.JSONArray;
import com.alonew.json.JSONException;
import com.alonew.json.JSONObject;
import com.cat.model.biz.logic.BasLogic;
import com.cat.model.biz.service.api.IndexService;
import com.cat.model.common.model.common.BannerVO;
import com.cat.model.common.model.common.BasConfigVO;
import com.cat.model.common.model.common.ModelInfoVO;
import com.cat.model.common.model.common.SolutionModelVO;
import com.cat.model.common.util.Constant;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.bean.BannerBean;
import com.cat.model.web.bean.IndexRecomBean;

@Service("indexApiService")
public class IndexServiceImpl implements IndexService{
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private BasLogic basLogic; 
	
	
	@Override
	public String bannerList(String cityCode){
		List<BannerVO> bannerList = modelMapper.getBannerList(cityCode);
		
		List<BannerBean> beanList = new ArrayList<BannerBean>();
		JSONObject json = ErrorCode.OK.responseJSON();
		if(bannerList == null || bannerList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		for(BannerVO vo : bannerList){			
			BannerBean bean = new BannerBean();
			bean.setName(vo.getName());
			bean.setImage(ImageUtil.wrapImageUrl(vo.getImage()));
			bean.setUrl(vo.getUrl());
			bean.setRichDesc(vo.getRichDesc());
			
			beanList.add(bean);
		}
		
		try {
			json.put("data", new JSONArray(beanList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
	@Override
	public String indexRecomList(String cityCode){
		List<IndexRecomBean> recomList = modelMapper.getIndexRecomList(cityCode);
		
		List<IndexRecomBean> beanList = new ArrayList<IndexRecomBean>();
		JSONObject json = ErrorCode.OK.responseJSON();
		if(recomList == null || recomList.size() <= 0){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				return ErrorCode.SERVER_ERROR.response();
			}
			return json.toString();
		}
		
		Date date = new Date();
		for(IndexRecomBean vo : recomList){
			//不在维护时间区间内则不展现
			if(vo.getArriveStartTime()!=null&&vo.getArriveStartTime().compareTo(date) > 0){
				continue;
			}
			if(vo.getArriveEndTime()!=null&&vo.getArriveEndTime().compareTo(date) < 0){
				continue;
			}
			
			IndexRecomBean bean = new IndexRecomBean();
			bean.setModelId(vo.getModelId());
			bean.setImage(ImageUtil.wrapImageUrl(vo.getImage()));
			bean.setModelName(vo.getModelName());
			bean.setAge(vo.getAge());
			bean.setSex(vo.getSex());
			bean.setModelCityCode(vo.getModelCityCode());
			bean.setCountryCode(vo.getCountryCode());
			bean.setPrice(vo.getPrice());
			bean.setModelCode(vo.getModelCode());
			
			beanList.add(bean);
		}
		
		try {
			json.put("data", new JSONArray(beanList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}

	/**
	 * 即将到达模特
	 */
	public String indexSoonArriveList(String cityCode,Integer page,Integer size){
		JSONObject json = ErrorCode.OK.responseJSON();
		List<IndexRecomBean> beanList =new ArrayList<IndexRecomBean>();
		
		//获取对应城市的省份
		BasConfigVO basConfigVO=basLogic.getOneConfigByName(cityCode);
		if(basConfigVO==null){
			try {
				json.put("data", new JSONArray(beanList));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json.toString();
		}
		
		//获取方案模特
		List<SolutionModelVO> modelVOList=modelMapper.getSolutionModelList(basConfigVO.getConfigValue(),(page-1)*size,size);
		for (SolutionModelVO solutionModelVO : modelVOList) {
			ModelInfoVO vo=modelMapper.getModelInfo(solutionModelVO.getModelId());
			
			IndexRecomBean bean = new IndexRecomBean();
			bean.setModelId(vo.getId());
			bean.setImage(ImageUtil.wrapImageUrl(vo.getCoverImage()));
			bean.setModelName(vo.getName());
			bean.setAge(vo.getAge());
			bean.setSex(vo.getSex());
			bean.setModelCityCode(vo.getCityCode());
			bean.setCountryCode(vo.getCountryCode());
			bean.setPrice(vo.getPrice());
			bean.setModelCode(vo.getModelCode());
			bean.setArriveStartTime(solutionModelVO.getWorkStartTime());
			bean.setArriveEndTime(solutionModelVO.getWorkEndTime());
			beanList.add(bean);
		}
		try {
			json.put("data", new JSONArray(beanList));
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
	
	
	
	@Override
	public String contactInfo() {
		List<BasConfigVO> contactList =   basLogic.getBasConfigByName(Constant.CONFIG_NAME_CONTACT_INFO);
		
		BasConfigVO basVO = contactList.get(0);
		
		JSONObject json = ErrorCode.OK.responseJSON();
		try {
			JSONObject infoJson = new JSONObject();
			infoJson.put("contactInfo", basVO.getConfigValue());
			
			json.put("data", infoJson);
		} catch (JSONException e) {
			return ErrorCode.SERVER_ERROR.response();
		}
		
		return json.toString();
	}
	
}
